// 泛型
// 泛型是具体类型或者其他属性的抽象代替,用于减少代码的重复

// 泛型分类

// 结构体定义中的泛型
struct Point<T> {
    x: T,
    y: T,
}

fn main() {
    let point = Point {
        x: 1,
        y: 2,
    };
    println!("{0}:{1}", point.x, point.y);
}


// 枚举中定义的泛型
enum Options<T, U> {
    Msg1(T),
    Msg2(U),
}

fn main() {
    let options1: Options<i32, String> = Options::Msg1(i32);
}

// 方法中定义的泛型
impl<T> Point<T> {
    fn get_x(&self) -> &T {
        &self.x
    }
    fn get_y(&self) -> &T {
        &self.y
    }
}

fn main() {
    let point = Point {
        x: 1,
        y: 2,
    };
    println!("{0}:{1}", point.x, point.y);
    println!("p.x=", point.get_x());
}
// 方法中的泛型不一定与结构体的泛型一样
impl<T1> Point<T1> {
    fn mix_up<V, U>(self, other: Point<V, U>) -> Point<V, U> {
        Point {
            x: self.x,
            y: other.y,
        }
    }
}

fn main() {
    let point = Point {
        x: 1,
        y: 2,
    };
    println!("{0}:{1}", point.x, point.y);
    let p3 = point.mix_up(point);
    println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
}
